<!--
 * 系统配置
 * @author luguoxiang
 * @date 2023-03-27
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
-->
<template>
  <edit-container ref="containerRef" v-bind="containerConfig">
    <lg-form ref="formRef" v-loading="loading" :schema="schema" v-bind="formConfig">
      <template #editor="{ data }">
        <t-textarea v-model="data.value" autosize />
      </template>
    </lg-form>
  </edit-container>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { EditContainer, useEditContainer } from '@/components';
import { LgForm, jsonFormat, parseJson } from '@/soar';
import schema from './schema';
import api from '@/api/sysConfig';


const emits = defineEmits(['ok']);

const language = ref('json');

const containerRef = ref();
const formRef = ref();
const { containerConfig, formConfig, loading, open } = useEditContainer<any>({
  data: {
    type: 1,
  },
  header: '系统配置',
  containerRef,
  formRef,
  isUpdate: (params: any) => params?.code,
  getData: (params: any) => {
    return api.detail(params?.code).then(res => {
      try {
        res.value = jsonFormat(JSON.parse(res.value));
      } catch (e) {
        return res;
      }
      return res;
    });
  },
  onOk: async ({ values, isUpdate, changed }) => {
    if (changed) {
      values.value = JSON.stringify(parseJson(values.value));
      await api.saveOrUpdate(values, isUpdate);
      emits('ok', values);
    }
  },
  containerProps: {
    width: '800px',
  },
});

defineExpose({ open });
</script>
